package main.java.tmi.main.web;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.main.service.MainService;
import main.java.tmi.main.service.MainVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class MainController {
	
	@Resource(name = "mainServiceImpl")
	private MainService mainService;

    /**
     * The Constructor
     */
    public MainController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/main")
    public String viewMain(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============viewMain start=========================");
        System.out.println("====================================================");
        List<MainVO> menuList = mainService.listMenu();
        List<MainVO> noticeList = mainService.listNotice();
        /*ModelAndView mav = new ModelAndView();*/
        model.addAttribute("menuList",menuList);
        model.addAttribute("noticeList",noticeList);
        return "/tmi/main";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/mainFrame")
    public String viewMainFrmae(HttpServletRequest request, HttpServletResponse response,
            ModelMap model, String menuId) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============viewMainFrmae start==================");
        System.out.println("====================================================");
        System.out.println("=======================menuId=============================" + menuId);
        model.addAttribute("menuId",menuId);
        return "/layouts/mainFrame";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/leftFrame")
    public String viewLeftFrame(HttpServletRequest request, HttpServletResponse response,
            ModelMap model, String menuId) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============viewLeftFrame start==================");
        System.out.println("====================================================");
        System.out.println("=======================menuId=============================" + menuId);
        
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
        List<MainVO> menuList = mainService.listMenuAll();
        model.addAttribute("menuList",menuList);
        model.addAttribute("userInfo",userInfo);
        model.addAttribute("menuId",menuId);
        return "/layouts/leftFrame";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/contentsFrame")
    public String viewContentsFrame(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("=============viewContentsFrame start================");
        System.out.println("====================================================");
        return "/layouts/contentsFrame";
    }
    
	/**
	 * �α��� ����
	 * 
	 * @param ModelMap model
	 * @param loginId �α���ID
	 * @param mainPassword ����ں�й�ȣ
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return : �α��� ��� ���� ó��
	 * @exception Exception
	 */
	@RequestMapping(value = "/main/listMenu")
	public ModelAndView login(@RequestParam Map param, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<MainVO> menuList = mainService.listMenu();
		ModelAndView mav = new ModelAndView();
		mav.addObject("menuList",menuList);
		mav.setViewName("/tmi/main");
		return mav;
	}
}
